#include "flownode.h"
#include "flowedge.h"

FlowNode::FlowNode()
    : isSelected_(false)
    , isVoid_(false)
{

}

FlowNode::~FlowNode()
{

}

void FlowNode::setSelected(bool isSet)
{
    bool changed = (isSet!=isSelected_);
    isSelected_ = isSet;
    if(changed) update();
}

void FlowNode::setVoid(bool isVoid)
{
    bool changed = (isVoid!=isVoid_);
    isVoid_ = isVoid;
    if(changed) update();
}

void FlowNode::addEdge(FlowEdge *edge)
{
    edgeList << edge;
    edge->adjust();
}

QList<FlowEdge *> FlowNode::edges() const
{
    return edgeList;
}

bool FlowNode::removeEdge(FlowEdge *edge)
{
    return edgeList.removeOne(edge);
}

void FlowNode::calculateForces()
{
    if (!scene() || scene()->mouseGrabberItem() == this) {
        newPos = pos();
        return;
    }

    // Sum up all forces pushing this item away
    qreal xvel = 0;
    qreal yvel = 0.1;
    //qreal yvel = 0.1;  //带重力效果.
    foreach (QGraphicsItem *item, scene()->items()) {
        if (item->type()!=ItemNode) continue;
        if (item == (QGraphicsItem *)this) continue;  //自己不算.
        QPointF vec = mapToItem(item, 0, 0);


        qreal dx = vec.x();
        qreal dy = vec.y();
        qreal len2 = dx*dx + dy*dy;
        if(len2<400) len2 = 400;
        qreal len = qSqrt(len2);
        //qDebug()<<"len-"<<len;
        if(len>50) continue;
        //万有引力 f=Gm1m2/r2
        xvel += (dx * (1200/len2) / len);
        yvel += (dy * (1200/len2) / len);

    }

    // Now subtract all forces pulling items together
    //double weight = (edgeList.size() + 1) * 10;
    //double weight = 100;
    foreach (FlowEdge *edge, edgeList) {
        QPointF vec;
        if (edge->sourceNode() == this)
            vec = mapToItem(edge->destNode(), 0, 0);
        else
            vec = mapToItem(edge->sourceNode(), 0, 0);
        qreal dx = vec.x();
        qreal dy = vec.y();
        qreal len2 = dx*dx + dy*dy;
        qreal len = qSqrt(len2);
        if(len>80) len = 80;
        len = 110 - len; //翻转用20~60映射80~40
        len2 = len*len;
        xvel -= (dx * (1200/len2) / len);
        yvel -= (dy * (1200/len2) / len);
        //xvel -= vec.x() / weight;
        //yvel -= vec.y() / weight;
    }

    if (qAbs(xvel) < 0.5 && qAbs(yvel) < 0.5)
        xvel = yvel = 0;

    QRectF sceneRect = scene()->sceneRect();
    //qDebug()<<"move node"<<id()<<QPointF(xvel, yvel);
    newPos = pos() + QPointF(xvel, yvel);
    newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
    newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
}

bool FlowNode::advance()
{
    if (newPos == pos())
        return false;

    setPos(newPos);
    foreach (FlowEdge *edge, edgeList)
        edge->adjust();
    return true;
}

QRectF FlowNode::boundingRect() const
{
    qreal adjust = 2;
    return QRectF( -10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust);
}

QPainterPath FlowNode::shape() const
{
    QPainterPath path;
    path.addEllipse(-12, -12, 25, 25);
    return path;
}


void FlowNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    painter->setRenderHint(QPainter::Antialiasing);
    if(isVoid_==false){
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::darkGray);
        painter->drawEllipse(-7, -7, 20, 20);
    }

    QRadialGradient gradient(-3, -3, 10);
    //if (option->state & QStyle::State_Sunken) {
    if(isSelected_){
        gradient.setCenter(3, 3);
        gradient.setFocalPoint(3, 3);
        gradient.setColorAt(1, QColor(Qt::yellow).light(120));
        gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
    } else {
        gradient.setColorAt(0, Qt::yellow);
        gradient.setColorAt(1, Qt::darkYellow);
    }
    QPen pen(Qt::black);
    if(isVoid_==false || isSelected_){
        painter->setBrush(gradient);  //球体涂色
    }
    else{
        pen.setStyle(Qt::DotLine);
    }

    painter->setPen(pen);
    painter->drawEllipse(-10, -10, 20, 20); //轮廓线
    //painter->drawEllipse(this->shape().boundingRect());
}
